<?php

/**
 * 这是一个表 "member" 的模型类。
 *
 * 表 'member' 有以下字段:
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $number
 * @property string $phone
 * @property string $service
 * @property integer $is_float
 * @property string $create_time
 * @property string $role
 *
 * 以下是表的关联关系:
 * @property LoginLog[] $loginLogs
 */
class Member extends CActiveRecord
{
	/**
	 * @return string 表名
	 */
	public function tableName()
	{
		return 'member';
	}
        
        public $old_password;
        public $new_password;
        public $repeat_password;

	/**
	 * @return array 表字段验证规则的数组。
	 */
	public function rules()
	{
		return array(
                        array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'),
                        array('old_password', 'findPasswords', 'on' => 'changePwd'),
                        array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'),
                    
			array('is_float', 'numerical', 'integerOnly'=>true),
			array('username, role,phone', 'length', 'max'=>20),
			array('password, service', 'length', 'max'=>255),
			array('number', 'length', 'max'=>50),
			array('create_time', 'safe'),
			// 以下规则用于验证查询函数，如果不做查询可注释此行。
			array('id, role, username, password, number, phone, service, is_float, create_time', 'safe', 'on'=>'search'),
		);
	}
        
        
        public function findPasswords($attribute, $params)
        {
            $Member = Member::model()->findByPk(Yii::app()->user->id);
            if ($Member->password != md5($this->old_password))
                $this->addError($attribute, '旧密码不正确.');
        }

	/**
	 * @return array 关联关系。
	 */
	public function relations()
	{
		return array(
			'loginLogs' => array(self::HAS_MANY, 'LoginLog', 'user_id'),
		);
	}

	/**
	 * @return array 自定义字段显示文本标签 (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '序号',
			'username' => '操作员',
			'password' => '密码',
			'number' => '工号',
			'phone' => '手机号',
			'service' => '业务归属',
			'is_float' => '状态',
			'create_time' => '创建时间',
                        'old_password' => '旧密码',
                        'new_password' => '新密码',
                        'repeat_password' => '密码确认',
                        'role'=>'权限名称',
		);
	}

	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('number',$this->number,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('service',$this->service,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->order='id desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Member the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
